import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/user'

export const useUserStore = defineStore('user', () => {
  const userInfo = ref(null)
  const token = ref(localStorage.getItem('token'))

  // 设置token
  const setToken = (newToken) => {
    token.value = newToken
    localStorage.setItem('token', newToken)
  }

  // 设置用户信息
  const setUserInfo = (info) => {
    userInfo.value = info
  }

  // 获取用户信息
  const fetchUserInfo = async () => {
    try {
      // 模拟获取用户信息
      const mockUserInfo = {
        id: 1,
        phone: '13800138000',
        nickname: '用户8000',
        avatar: 'https://placeholder.com/150'
      }
      userInfo.value = mockUserInfo
    } catch (error) {
      console.error('获取用户信息失败：', error)
    }
  }

  // 退出登录
  const logout = () => {
    token.value = null
    userInfo.value = null
    localStorage.removeItem('token')
  }

  return {
    userInfo,
    token,
    setToken,
    setUserInfo,
    fetchUserInfo,
    logout
  }
}) 